<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Transaksi_akun_model extends CI_Model {
	
	
	public function __construct()
	{
		parent::__construct();
	}

	
	// fungsi menambah transaksi_akun (add)
	function tambah_transaksi_akun($data)
	{
		$this->db->insert('TRANSAKSI_AKUN', $data);
	}

        // fungsi mengubah data transaksi_akun (edit)
	function ubah_transaksi_akun($data, $id_transaksi_akun)
	{
            $this->db->where('ID_TRANSAKSI_AKUN', $id_transaksi_akun);
            $this->db->update('TRANSAKSI_AKUN', $data);
	}

        // fungsi mengubah data transaksi_akun (edit)
	function ubah_transaksi_akun_byStatus($data, $id_transaksi, $status)
	{
            $this->db->where('ID_TRANSAKSI', $id_transaksi);
            $this->db->where('STATUS_NOTE', $status);
            $this->db->update('TRANSAKSI_AKUN', $data);
	}

        // fungsi mengubah data transaksi_akun (edit)
	function hapus_transaksi_akun_byStatus($id_transaksi, $status)
	{
            $this->db->where('ID_TRANSAKSI', $id_transaksi);
            $this->db->where('STATUS_NOTE', $status);
            $this->db->delete('TRANSAKSI_AKUN');
	}

        // fungsi menghapus data transaksi_akun
	function hapus_transaksi_akun($id_transaksi_akun)
	{
            $this->db->where('ID_TRANSAKSI_AKUN', $id_transaksi_akun);
            $this->db->delete('TRANSAKSI_AKUN');
	}
	
	//fungsi query untuk jqgrid
	function getAllGrid($start,$limit,$sidx,$sord,$where){
		$this->db->select('*, if(STATUS="1","Aktif","Tidak aktif") as STATUS2',false);
		$this->db->limit($limit);
		if($where != NULL)$this->db->where($where,NULL,FALSE);
		$this->db->order_by($sidx,$sord);
		$query = $this->db->get('TRANSAKSI_AKUN',$limit,$start);

		return $query->result();
        }

        // fungsi untuk mendapatkan transaksi_akun terakhir
	function getLast_transaksi_akun(){
            $this->db->select_max('ID_TRANSAKSI_AKUN');
            $this->db->from('TRANSAKSI_AKUN');

            return $this->db->get();
        }

        // fungsi untuk mendapatkan transaksi_akun berdasarkan id
        function get_transaksi_akun_byID($id_transaksi_akun){
            $this->db->select('*');
            $this->db->from('TRANSAKSI_AKUN');
            $this->db->where('ID_TRANSAKSI_AKUN', $id_transaksi_akun);

            return $this->db->get();
        }

        // mendapatkan data transaksi akun berdasarkan status(debet, kredit) dan id transaksinya
        function get_transaksi_byStatus($id_transaksi, $status){
            $this->db->select('*');
            $this->db->from('TRANSAKSI_AKUN');
            $this->db->where('ID_TRANSAKSI', $id_transaksi);
            $this->db->where('STATUS_NOTE', $status);

            return $this->db->get();
        }

        // mendapatkan data laporan buku besar
        function get_laporan_buku_besar($tgl_awal, $tgl_akhir,$id_batch){
            $this->db->select('*');
            $this->db->from('TRANSAKSI_AKUN ta');
            $this->db->join('AKUN','AKUN.ID_AKUN=ta.ID_AKUN');
            $this->db->join('TRANSAKSI_KEUANGAN tk','tk.ID_TRANSAKSI=ta.ID_TRANSAKSI');
            $this->db->where("tk.TANGGAL_TRANSAKSI between '$tgl_awal' AND '$tgl_akhir'");
			$this->db->where('tk.ID_BATCH',$id_batch);
            $this->db->order_by('ta.ID_AKUN');
            $this->db->order_by('TANGGAL_TRANSAKSI');

            return $this->db->get();
        }

        // mendapatkan nilai total transaksi berdasarkan id akun
        function get_total_transaksi_byAkun($id_akun, $status, $tgl_awal, $tgl_akhir){
            $this->db->select('AKUN.*');
            $this->db->select_sum('JUMLAH_TRANSAKSI','TOTAL_TRANSAKSI');
            $this->db->from('AKUN');
            $this->db->join('TRANSAKSI_AKUN ta','AKUN.ID_AKUN=ta.ID_AKUN', 'left');
            $this->db->join('TRANSAKSI_KEUANGAN tk','tk.ID_TRANSAKSI=ta.ID_TRANSAKSI', 'left');
            $this->db->where('AKUN.ID_AKUN', $id_akun);
            $this->db->where('STATUS_NOTE', $status);
            $this->db->where("tk.TANGGAL_TRANSAKSI between '$tgl_awal' AND '$tgl_akhir'");
            $this->db->group_by('AKUN.ID_AKUN');

            return $this->db->get();
        }

        // mendapatkan nilai total transaksi berdasarkan id akun
        function get_transaksi_byAkunParent($id_akun){
            $this->db->select('AKUN.*');
            $this->db->from('AKUN');
            $this->db->join('TRANSAKSI_AKUN ta','AKUN.ID_AKUN=ta.ID_AKUN', 'left');
            $this->db->join('TRANSAKSI_KEUANGAN tk','tk.ID_TRANSAKSI=ta.ID_TRANSAKSI', 'left');
            $this->db->where('ID_PARENT', $id_akun);
            $this->db->group_by('AKUN.ID_AKUN');

            return $this->db->get();
        }

        // mendapatkan nilai total transaksi berdasarkan id akun
        function get_transaksi_byAkun($id_akun){
            $this->db->select('*');
            $this->db->select_sum('JUMLAH_TRANSAKSI','TOTAL_TRANSAKSI');
            $this->db->from('AKUN');
            $this->db->join('TRANSAKSI_AKUN ta','AKUN.ID_AKUN=ta.ID_AKUN', 'left');
            $this->db->where('AKUN.ID_AKUN', $id_akun);
            $this->db->group_by('AKUN.ID_AKUN');

            return $this->db->get();
        }
}